//
//  SettingViewController.m
//  ZYZB
//
//  Created by ENERGY on 2018/7/11.
//  Copyright © 2018年 ENERGY. All rights reserved.
//

#import "SettingViewController.h"
#import "SettingContentTableViewCell.h"
#import "SettingArrowTableViewCell.h"
#import "SettingSwitchTableViewCell.h"
#import "BankCardManageViewController.h"
#import "SafetyManageViewController.h"
#import "AboutFLZViewController.h"
#import "FeedBackViewController.h"
@interface SettingViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong)UITableView * tableView;
@property (nonatomic, strong)UIView * footerView;
@end

@implementation SettingViewController
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"设置";
    //返回按钮
    UIButton * backBtn = [[UIButton alloc] initWithFrame:CGRectMake(18/WIDTH_6_SCALE, 14/WIDTH_6_SCALE + 24 + 20, 24/WIDTH_6_SCALE, 30/WIDTH_6_SCALE)];
    [backBtn addTarget:self action:@selector(backBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [backBtn setImage:[UIImage imageNamed:@"out"] forState:UIControlStateNormal];
    UIBarButtonItem * leftItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
    self.navigationItem.leftBarButtonItem = leftItem;
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    [self.view addSubview:self.tableView];
}
#pragma mark - init
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - BOTTOM_HEIGHT - NAVI_HEIGHT) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView registerClass:[SettingArrowTableViewCell class] forCellReuseIdentifier:NSStringFromClass([SettingArrowTableViewCell class])];
        [_tableView registerClass:[SettingContentTableViewCell class] forCellReuseIdentifier:NSStringFromClass([SettingContentTableViewCell class])];
        [_tableView registerClass:[SettingSwitchTableViewCell class] forCellReuseIdentifier:NSStringFromClass([SettingSwitchTableViewCell class])];
        _tableView.tableFooterView = self.footerView;
        _tableView.backgroundColor = getColor(grayBGColor);
    }return _tableView;
}
- (UIView *)footerView{
    if (!_footerView) {
        _footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 100/WIDTH_6_SCALE)];
        int btnX = 57/WIDTH_6_SCALE;
        UIView * lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 1/WIDTH_6_SCALE)];
        lineView.backgroundColor = getColor(grayColor);
        [_footerView addSubview:lineView];
        UIButton * quitBtn = [[UIButton alloc] initWithFrame:CGRectMake(btnX, 55/WIDTH_6_SCALE, SCREEN_WIDTH - btnX*2, 38/WIDTH_6_SCALE)];
        [self button:quitBtn withBGColor:getColor(@"3675bd") withLayerBorderColor:nil withLayerBorderWith:0 withCornerRadius:6 withMasksToBounds:YES withViwe:_footerView];
        [quitBtn setTitle:@"退出登录" forState:UIControlStateNormal];
        [quitBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [quitBtn addTarget:self action:@selector(quitBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        quitBtn.titleLabel.font = DEF_FontSize_12;
    }return _footerView;
}
#pragma mark - tableViewDelegate和tableViewDataSource
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell * cell = nil;
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            SettingContentTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([SettingContentTableViewCell class]) forIndexPath:indexPath];
            if (self.ifHaveOpenAccount == 0) {
                cell.weChatLab.text = @"去开户";
                cell.weChatLab.textColor = getColor(@"ff8819");
            }
            if (self.ifHaveOpenAccount == 1 || self.ifHaveOpenAccount == 2) {
                cell.weChatLab.text = @"";
                cell.weChatLab.textColor = [UIColor blackColor];
            }
            return cell;
        }else if (indexPath.row == 1){
            SettingArrowTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([SettingArrowTableViewCell class]) forIndexPath:indexPath];
            cell.lineView.hidden = YES;
            cell.titleLab.text = @"安全管理";
            return cell;
        }
    }else if (indexPath.section == 1){
        if (indexPath.row == 0) {
            SettingSwitchTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([SettingSwitchTableViewCell class]) forIndexPath:indexPath];
            return cell;
        }else{
            SettingArrowTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([SettingArrowTableViewCell class]) forIndexPath:indexPath];
            if (indexPath.row == 1) {
                cell.titleLab.text = @"关于福乐猪";
            }else{
                cell.titleLab.text = @"用户反馈";
                cell.lineView.hidden = YES;
            }
            return cell;
        }
    }return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 2;
    }else{
        return 3;
    }
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 56/WIDTH_6_SCALE;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView * sectionView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 15/WIDTH_6_SCALE)];
    sectionView.backgroundColor = getColor(grayBGColor);
    UIView * topLineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 1/WIDTH_6_SCALE)];
    topLineView.backgroundColor = getColor(grayColor);
    [sectionView addSubview:topLineView];
    UIView * bottomLineView = [[UIView alloc] initWithFrame:CGRectMake(0, sectionView.bottom - 1/WIDTH_6_SCALE, SCREEN_WIDTH, 1/WIDTH_6_SCALE)];
    bottomLineView.backgroundColor = getColor(grayColor);
    [sectionView addSubview:bottomLineView];
    return sectionView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return 0;
    }return 15/WIDTH_6_SCALE;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            BankCardManageViewController * bankCardManageVC = [[BankCardManageViewController alloc] init];
            bankCardManageVC.ifHaveOpenAccount = self.ifHaveOpenAccount;
            [self.navigationController pushViewController:bankCardManageVC animated:YES];
        }
        if (indexPath.row == 1) {
            SafetyManageViewController * safetyManageViewVC = [[SafetyManageViewController alloc] init];
            [self.navigationController pushViewController:safetyManageViewVC animated:YES];
        }
    }else{
        if (indexPath.row == 1) {
            AboutFLZViewController * aboutFLZVC = [[AboutFLZViewController alloc] init];
            [self.navigationController pushViewController:aboutFLZVC animated:YES];
        }
        if (indexPath.row == 2) {
            FeedBackViewController * feedBackVC = [[FeedBackViewController alloc] init];
            [self.navigationController pushViewController:feedBackVC animated:YES];
        }
    }
}
#pragma mark - action
- (void)backBtnClick:(UIButton *)sender{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)quitBtnClick:(UIButton *)sender{
    UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"确定退出登录？"
                                                                   message:nil
                                                            preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
                                                          handler:^(UIAlertAction * action) {
                                                              //响应事件
//                                                              NSLog(@"action = %@", action);
                                                              [UserModel sharedInstanced].mobile = @"";
                                                          }];
    UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault
                                                         handler:^(UIAlertAction * action) {
                                                             //响应事件
                                                             NSLog(@"action = %@", action);
                                                         }];
    
    [alert addAction:defaultAction];
    [alert addAction:cancelAction];
    [self presentViewController:alert animated:YES completion:nil];
}
@end
